﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VoltGameEditor
{
	public partial class TexturePanel : Control
	{
		Image _image = null;

		public Image Texture
		{
			get { return _image; }
			set
			{
				_image = value;

				UpdateTexture();
			}
		}

		Size _resizeSize;

		public Size ResizeSize
		{
			get { return _resizeSize; }
			set
			{
				if (_autoSize)
					return;

				_resizeSize = value;
				UpdateTexture();
			}
		}

		bool _displaySize = false;

		public bool DisplaySize
		{
			get { return _displaySize; }
			set { _displaySize = value; }
		}

		new public Size Size
		{
			get { return base.Size; }
			set
			{
				if (_autoSize || _resizeSize.Width == 0 && _resizeSize.Height == 0)
				{
					int smallest = Math.Min(value.Width, value.Height);
					_resizeSize = new Size(smallest, smallest);
				}

				base.Size = value;

				UpdateTexture();
			}
		}

		Size _realImageSize;
		string _sizeString;

		private void UpdateTexture()
		{
			if (_image == null)
				return;

			int imgWidth = _image.Width;
			int imgHeight = _image.Height;
			float aspect = ((float)imgWidth / (float)imgHeight);

			if (imgWidth > _resizeSize.Width ||
				imgHeight > _resizeSize.Height)
			{
				if (imgWidth > imgHeight)
				{
					int amt = imgWidth - _resizeSize.Width;
					imgWidth -= amt;
					imgHeight -= (int)((float)amt / aspect);
				}
				else
				{
					int amt = imgHeight - _resizeSize.Height;
					imgWidth -= (int)((float)amt * aspect);
					imgHeight -= amt;
				}
			}

			_realImageSize.Width = imgWidth;
			_realImageSize.Height = imgHeight;

			_sizeString = "("+_image.Width.ToString()+"x"+_image.Height.ToString()+")";
		}

		bool _autoSize = true;

		public override bool AutoSize
		{
			get { return _autoSize; }
			set { _autoSize = value; }
		}

		Color _selectColor = SystemColors.Highlight;
		SolidBrush _selectBrush;

		public Color SelectColor
		{
			get { return _selectColor; }
			set
			{
				_selectColor = value;
				_selectBrush = new SolidBrush(_selectColor);
			}
		}

		StringFormat _stringFormat = new StringFormat();
		public TexturePanel()
		{
			_stringFormat.Alignment = StringAlignment.Center;
			_selectBrush = new SolidBrush(_selectColor);
			_stringBrush = new SolidBrush(base.ForeColor);
		}

		protected override void OnMouseClick(MouseEventArgs e)
		{
			Focus();
			Invalidate();
			base.OnMouseClick(e);
		}

		protected override void OnGotFocus(EventArgs e)
		{
			Invalidate();
			base.OnGotFocus(e);
		}

		protected override void OnLostFocus(EventArgs e)
		{
			Invalidate();
			base.OnLostFocus(e);
		}

		Border3DStyle _borderStyle = Border3DStyle.Bump;
		public Border3DStyle BorderStyle
		{
			get { return _borderStyle; }
			set { _borderStyle = value; }
		}

		bool _border = true;
		public bool Border
		{
			get { return _border; }
			set { _border = value; }
		}

		SolidBrush _stringBrush;
		public override Color ForeColor
		{
			get { return base.ForeColor; }
			set
			{
				base.ForeColor = value;
				_stringBrush = new SolidBrush(base.ForeColor);
			}
		}

		protected override void OnPaint(PaintEventArgs pe)
		{
			if (Focused)
				pe.Graphics.FillRectangle(_selectBrush, DisplayRectangle);

			if (_border)
				ControlPaint.DrawBorder3D(pe.Graphics, DisplayRectangle, _borderStyle);

			pe.Graphics.DrawString(Text, Font, _stringBrush, new PointF(Width / 2, Height - 30), _stringFormat);

			if (DisplaySize)
				pe.Graphics.DrawString(_sizeString, Font, _stringBrush, new PointF(Width / 2, Height - 16), _stringFormat);

			if (_image != null)
			{
				int sub = Math.Abs(Width - Height) / 2;
				pe.Graphics.DrawImage(_image, new Rectangle(new Point((Width / 2) - (_realImageSize.Width / 2), ((Height / 2) - sub) - (_realImageSize.Height / 2) + 2), new Size(_realImageSize.Width, _realImageSize.Height)));
			}

			base.OnPaint(pe);
		}
	}
}
